package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.atguigu.tingshu.album.config.MinioConstantProperties;
import com.atguigu.tingshu.album.service.AuditService;
import com.atguigu.tingshu.album.service.FileUploadService;
import com.atguigu.tingshu.common.execption.GuiguException;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import static com.atguigu.tingshu.common.constant.SystemConstant.*;

@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {

   @Autowired
   private MinioConstantProperties minioConstantProperties;
   @Autowired
   private MinioClient minioClient;
   @Autowired
   private AuditService auditService;

    //图片上传到Minio服务器
    @Override
    public String fileUpload(MultipartFile file) {
        try {
            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if(bufferedImage==null){
                throw new GuiguException(500,"错误图片");
            }
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if(width>900 || height>900){
                throw new GuiguException(500,"图片为900*900");
            }
            //审核图片
            String suggestion = auditService.auditImage(file);
            if ("review".equals(suggestion) || "block".equals(suggestion)) {
                throw new GuiguException(500,"图片内容不合法");
            }

            String folderName= "/"+DateUtil.today();
            String exName=FileUtil.extName(file.getOriginalFilename());
            String objectName=folderName+"/"+UUID.randomUUID().toString()+"."+exName;

            minioClient.putObject(
                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName())
                            .object(objectName)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType("video/mp4").build());

            return minioConstantProperties.getEndpointUrl()+"/"+minioConstantProperties.getBucketName()+objectName;
        } catch (Exception e) {
            log.error("图片上传失败",e);
            throw new GuiguException(500,e.getMessage());
        }
    }


    //图片上传到Minio服务器


//    @Override
//    public String fileUpload(MultipartFile file) {


//        try {
//            BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
//            if(bufferedImage==null){
//                throw new GuiguException(500,"错误的图片");
//            }
//            int height = bufferedImage.getHeight();
//            int width = bufferedImage.getWidth();
//            if(height>900 || width>900){
//                throw new GuiguException(500,"图片限定900*900");
//            }
//
//            String folderName = "/"+DateUtil.today();
//            String exName=FileUtil.extName(file.getOriginalFilename());
//            String objectName=folderName+"/"+UUID.randomUUID().toString()+"."+exName;
//            minioClient.putObject(
//                    PutObjectArgs.builder().bucket(minioConstantProperties.getBucketName())
//                            .object(objectName)
//                            .stream(file.getInputStream(), file.getSize(), -1)
//                            .contentType("video/mp4").build());
//            return minioConstantProperties.getEndpointUrl()+"/"+minioConstantProperties.getBucketName()+objectName;
//        } catch (Exception e) {
//            log.error("文件上传错误",e);
//            throw  new GuiguException(500,e.getMessage());
//        }

//    }

}
